package com.example.demo.use_demo.a_excel_demo;

import cn.hutool.core.exceptions.DependencyException;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.PoiChecker;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;

import java.io.InputStream;

/**
 * 扩展HuTool
 *
 * @author zhangqiang
 * @date 2021/3/31 16:33
 */
public class ExcelUtilExtHuTool extends ExcelUtil {
    /**
     * 获取Excel读取器，通过调用{@link ExcelReader}的read或readXXX方法读取Excel内容<br>
     * 默认调用第一个sheet，读取结束自动关闭流
     *
     * @param bookStream Excel文件的流
     * @return {@link ExcelReader}
     */
    public static <K> ExcelReaderExtHuTool<K> getReaderExtension(InputStream bookStream) {
        return getReaderExtension(bookStream, 0, true);
    }

    /**
     * 获取Excel读取器，通过调用{@link ExcelReader}的read或readXXX方法读取Excel内容
     *
     * @param bookStream     Excel文件的流
     * @param sheetIndex     sheet序号，0表示第一个sheet
     * @param closeAfterRead 读取结束是否关闭流
     * @return {@link ExcelReader}
     * @since 4.0.3
     */
    public static <K> ExcelReaderExtHuTool<K> getReaderExtension(InputStream bookStream, int sheetIndex, boolean closeAfterRead) {
        try {
            return new ExcelReaderExtHuTool<>(bookStream, sheetIndex, closeAfterRead);
        } catch (NoClassDefFoundError e) {
            throw new DependencyException(ObjectUtil.defaultIfNull(e.getCause(), e), PoiChecker.NO_POI_ERROR_MSG);
        }
    }
    // --------------------------------------------------zq extension---------------------------------------------------

    /**
     * 获取Excel读取器，通过调用{@link ExcelReader}的read或readXXX方法读取Excel内容<br>
     * 默认调用第一个sheet，读取结束自动关闭流
     *
     * @param bookStream Excel文件的流
     * @param clazz      实体类型
     * @return {@link ExcelReader}
     */
    public static <K> ExcelReaderExtHuTool<K> getReaderExtension(InputStream bookStream, Class<K> clazz) {
        return getReaderExtension(bookStream, 0, true, clazz);
    }

    /**
     * 获取Excel读取器，通过调用{@link ExcelReader}的read或readXXX方法读取Excel内容
     *
     * @param bookStream     Excel文件的流
     * @param sheetIndex     sheet序号，0表示第一个sheet
     * @param closeAfterRead 读取结束是否关闭流
     * @return {@link ExcelReader}
     * @since 4.0.3
     */
    public static <K> ExcelReaderExtHuTool<K> getReaderExtension(InputStream bookStream, int sheetIndex, boolean closeAfterRead, Class<K> clazz) {
        try {
            return new ExcelReaderExtHuTool<>(bookStream, sheetIndex, closeAfterRead, clazz);
        } catch (NoClassDefFoundError e) {
            throw new DependencyException(ObjectUtil.defaultIfNull(e.getCause(), e), PoiChecker.NO_POI_ERROR_MSG);
        }
    }
}
